﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Globalization;
using System.Web;
using System.Web.Services;
using OA.Bll;
using OA.Bll.Authority;
using OA.Model;
using OA.Utility;
namespace OA.Web.Handler
{
    /// <summary>
    /// $codebehindclassname$ 的摘要说明
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    public class LoginHandler : IHttpHandler, System.Web.SessionState.IRequiresSessionState
    {

        public void ProcessRequest(HttpContext context)
        {
            HttpResponse res = context.Response;
            context.Response.ContentType = "text/plain";
            //HttpRequest req = context.Request;
            bool bl ;
            string strmsg = "";
            string returnMsg = "";

            try
            {

                JsonReq req = JsonHelper.JsonDeserialize<JsonReq>(JsonHelper.GetPostString());
                string page = context.Request["page"];
                string rows = context.Request["rows"];
                if (!string.IsNullOrEmpty(page))
                {
                    req.Options.Add("page", page);
                }
                if (!string.IsNullOrEmpty(rows))
                {
                    req.Options.Add("rows", rows);
                }
                string userName = req.Options["UserName"];
                string password = Security.DESEncrypt(req.Options["Password"], "newfores");
                if (string.IsNullOrEmpty(password))
                {
                    password = req.Options["Password"];
                }
                UserInfoPresenter userInfoPresenter = new UserInfoPresenter();
                ViewUserRoleGroupInfo model =
                    userInfoPresenter.GetUser(" AND UserName='" + userName + "' AND Password='" + password + "'");
                if (model != null)
                {
                    HttpContext.Current.Session["username"] = model.UserName;
                    UserSession userinfo = new UserSession(Convert.ToInt32(model.UserID), model.UserName, model.RoleID,
                                                           Convert.ToInt32(model.Status));
                    SessionBox.CreateUserSession(userinfo);
                    bl = true;
                    strmsg = "登陆成功!";
                }
                else
                {
                    bl = false;
                    strmsg = "用户名或者密码错误!";
                }
                if (bl&&model .EmployeeId >0)
                {
                    returnMsg = "{ \"flag\":\"" + bl.ToString(CultureInfo.InvariantCulture) +
                                "\",\"msg\":\"" + strmsg + "\",\"EmployeeId\":\"" + model.EmployeeId + "\"}";
                }
                else
                {
                    returnMsg = "{ \"flag\":\"" + bl.ToString(CultureInfo.InvariantCulture) +
                                "\",\"msg\":\"" + strmsg + "\"}";
                }
            }
            catch (Exception ex)
            {
                Log.Logger.LogError(ex.Message);
            }
            context.Response.Write(returnMsg);
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}
